//
//  XDimageView.swift
//  进度视图
//
//  Created by HanXingda on 15/7/7.
//  Copyright © 2015年 韩兴达. All rights reserved.
//

import UIKit

class XDimageView: UIImageView {

    var progress: CGFloat = 0 {
        didSet {
            progressView.hidden = progress == 0
            progressView.progress = progress
        }
    }
    
   private lazy var progressView: XDprogressView = { [unowned self] in
        let v = XDprogressView()
        self.addSubview(v)
        v.ff_Fill(self)
        return v
    }()
    
    class XDprogressView: UIView {
        
        var progress: CGFloat = 0 {
            didSet {
                setNeedsDisplay()
            }
        }
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.clearColor()
        }
        
        required init(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override func drawRect(rect: CGRect) {
            //准备
            let r = min(rect.width, rect.height) * 0.4
            let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
            let ovalRect = CGRect(x: center.x - r, y: center.y - r, width: 2 * r, height: 2 * r)
            //画圆
            let path1 = UIBezierPath(ovalInRect: ovalRect)
            UIColor(white: 1, alpha: 0.5).setFill()
            path1.fill()
            //画圆弧
            let start = CGFloat(-M_PI_2)
            let end = CGFloat(2 * M_PI) * progress + start
            
            let path2 = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
            path2.addLineToPoint(center)
            path2.closePath()
            UIColor(white: 0.0, alpha: 0.5).setFill()
            path2.fill()
            
        }
    }

}

